package br.com.rapidrest.chain;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import br.com.rapidrest.exception.ChainException;

public class IgnoreCommand implements Command {

	private List<String> ignoreList;

	public IgnoreCommand() {
		ignoreList = new ArrayList<String>();
	}

	public IgnoreCommand(String... ignore) {
		ignoreList = new ArrayList<String>(Arrays.asList(ignore));
	}

	@Override
	public void execute(Chain chain) throws ChainException {
		HttpServletRequest request = chain.getContext().getRequest();

		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();
		requestURI = requestURI.replace(contextPath, "");

		for (String ignore : ignoreList) {
			if (requestURI.startsWith(ignore)) {
				chain.getCommands().clear();
				break;
			}
		}

		chain.proceed();
	}
	
	public void addIgnore(String ignore) {
		ignoreList.add(ignore);
	}

}
